﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;
using System.Text;
using Volo.Abp.DependencyInjection;

namespace TianShu.Untils.Validation
{
    public class ValidationUtils : IValidationUtils, ISingletonDependency
    {
        public bool Validation<T>(T obj, out string errorMsg) where T : class
        {
            errorMsg = "";
            var objType = typeof(T);

            var objProperties = objType.GetProperties();

            foreach (var item in objProperties)
            {
                string proName = item.Name;
                var proValue = item.GetValue(obj);
                var displayNameAttr = item.GetCustomAttribute<DisplayNameAttribute>(false);
                if (displayNameAttr != null)
                {
                    proName = displayNameAttr.DisplayName;
                }

                var requiredAttr = item.GetCustomAttribute<RequiredAttribute>(false);
                if (requiredAttr!=null)
                {
                    if (requiredAttr.IsValid(proValue) == false)
                    {
                        var requireErrorMsg = requiredAttr.ErrorMessage;
                        if (requireErrorMsg.IsNullOrEmpty())
                        {
                            requireErrorMsg = "不能为空";
                        }
                        errorMsg += $"【{proName}】{requireErrorMsg}，";
                    }
                }

                var stringLengthAttr = item.GetCustomAttribute<StringLengthAttribute>(false);
                if (stringLengthAttr!=null)
                {
                    if (stringLengthAttr.IsValid(proValue) == false)
                    {
                        var stringLengthErrorMsg = stringLengthAttr.ErrorMessage;
                        if (stringLengthErrorMsg.IsNullOrEmpty())
                        {
                            stringLengthErrorMsg = "长度只能在{0}到{1}之间";
                        }

                        errorMsg += $"【{proName}】{string.Format(stringLengthErrorMsg, stringLengthAttr.MinimumLength, stringLengthAttr.MaximumLength)}，";
                    }
                }

                var regularExpressionAttr = item.GetCustomAttribute<RegularExpressionAttribute>(false);
                if (regularExpressionAttr!=null)
                {
                    if (regularExpressionAttr.IsValid(proValue) == false)
                    {
                        var regularExpressionErrorMsg = regularExpressionAttr.ErrorMessage;
                        if (regularExpressionErrorMsg.IsNullOrEmpty())
                        {
                            regularExpressionErrorMsg = "不符合规则，";
                        }

                        errorMsg += $"【{proName}】{regularExpressionErrorMsg}，";
                    }
                }

                var rangeAttr = item.GetCustomAttribute<RangeAttribute>(false);
                if (rangeAttr!=null)
                {
                    if (rangeAttr.IsValid(proValue) == false)
                    {
                        var rangeErrorMsg = rangeAttr.ErrorMessage;
                        if (rangeErrorMsg.IsNullOrEmpty())
                        {
                            rangeErrorMsg = "大小只能在{0}到{1}之间";
                        }

                        errorMsg += $"【{proName}】{string.Format(rangeErrorMsg, rangeAttr.Minimum, rangeAttr.Maximum)}，";
                    }
                }
            }
            if (errorMsg.IsNullOrEmpty())
            {
                return true;
            }
            return false;
        }
    }
}
